package com.xiecong.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Slf4j
@Aspect
//@Component
public class RecordTimeAspect {
    //切入点方法（公共的切入点表达式）
    @Pointcut("execution(* com.xiecong.service.impl.DeptServiceImpl.*(..))")
    public void pt(){}
    //环绕通知
    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint pjp){
        long begin = System.currentTimeMillis();//Before
        Object result = null;
        try {
            result = pjp.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);//AfterThrowing
        } finally {
            log.info("业务执行结束");//After
        }
        //AfterReturning
        long end = System.currentTimeMillis();
        log.info("业务执行时间："+(end-begin)+"ms");
        return result;
    }
    //前置通知（引用切入点）
    @Before("pt()")
    public void before(JoinPoint joinPoint){
        log.info("before......");
    }
    //后置通知
    //@After("pt()")
    public void after(JoinPoint joinPoint){
        log.info("after......");
    }
    //返回后通知（程序在正常执行的情况下，会执行的后置通知）
    //@AfterReturning("pt()")
    public void afterReturning(JoinPoint joinPoint){
        log.info("afterReturning......");
    }
    //异常通知（程序在出现异常的情况下，执行的后置通知）
    //@AfterThrowing("pt()")
    public void afterThrowing(JoinPoint joinPoint){
        log.info("afterThrowing......");
    }
}
